'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 主城小玩法
 * @author sundbcn@gmail.com (sun debing)
 */

var moment = require('moment');
var _ = require('lodash');
var modelUtil = require('../util/modelUtil');
var mongoConnector = require('../libs/mongodb/mongoConnector');
/* jshint unused:false */
var huntSchema = require('../schemas/huntSchema.js');
var Hunt = mongoConnector().model('Hunt');
var constantUtil = require('../util/constantUtil');
var smallPlayConf = require('../config/smallPlayConf.js');

/**
 * 主城小玩法
 */
module.exports.getByRoleId = function (role_id, cb) {
    modelUtil.getByRoleId(Hunt, role_id, function (err, hunt_model) {
        if (!!err) {
            console.error(err);
            return cb(err);
        }
        cb(null, hunt_model);
    });
};

/**
 * 刷新数据
 */
module.exports.onRefreshData = function (self, cb) {
    _refresh_hunt(self);
    cb();
};

// 刷新操作
var _refresh_hunt = function (hunt_model) {
    // 外城猎物刷新
    var hunt_refresh_time = moment(hunt_model.hunt_time).add(constantUtil.value('hunt_monster_fresh_time'), 's');
    // var hunt_refresh_time = moment(hunt_model.hunt_time).add(10, 's');
    if (hunt_refresh_time <= moment()) {
        var hunt_list = smallPlayConf.getHuntList();
        var random_num = _.random(0, hunt_list.length - 1);
        var hunt_only = hunt_list[random_num];
        hunt_model.setHunt(hunt_only);
    }

    // 主城宝箱
    var chest_refresh_time = moment(hunt_model.chest_time).add(constantUtil.value('city_box_fresh_time'), 's');
    if (chest_refresh_time <= moment()) {
        hunt_model.setChest();
    }
};
